package com.jadesoft.listener;

import java.util.Calendar;
import java.util.Timer;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.jadesoft.stock.util.Constants;

public class WeatherListener implements ServletContextListener {
	private Timer timer = null;

	public void contextInitialized(ServletContextEvent arg0) {
		timer = new Timer(true);
		Calendar current = Calendar.getInstance(Constants.TIME_ZONE_CHINA);
		Calendar execute = Calendar.getInstance(Constants.TIME_ZONE_CHINA);
		execute.set(Calendar.HOUR_OF_DAY, Constants.WEATHER_LISTENER_HOUR);
		execute.set(Calendar.MINUTE, Constants.WEATHER_LISTENER_MINUTE);
		if (execute.before(current)) {
			execute.add(Calendar.DATE, 1);
		}
		long delay = execute.getTimeInMillis() - current.getTimeInMillis();

		timer.schedule(new WeatherTask(), delay, 24 * 60 * 60 * 1000);
	}

	public void contextDestroyed(ServletContextEvent arg0) {
		timer.cancel();
	}

}
